package com.zmn.oms.zmn.business.impl.work.list;

import com.google.common.collect.Lists;
import com.zmn.brs.common.enums.EventEnum;
import com.zmn.brs.common.enums.RecordFieldEnum;
import com.zmn.brs.starter.annotation.RecordField;
import com.zmn.brs.starter.annotation.StaffOperateRecord;
import com.zmn.common.constant.FicoConsts;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.business.interfaces.work.OrderWorkListBService;
import com.zmn.oms.model.entity.work.OrderWorkQuery;
import com.zmn.base.oms.common.model.es.orderwork.EsOrderWork;
import com.zmn.oms.model.vo.work.list.ZsOrderWorkHqFinanceVO;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkQueryBService;
import com.zmn.oms.zmn.business.interfaces.work.list.ZsOrderWorkHqFinanceQueryBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 类描述：总公司财务列表查询
 *
 * @author liuying
 * @date 2018/12/27 12:02
 */
@Service
public class ZsOrderWorkHqFinanceQueryBServiceImpl implements ZsOrderWorkHqFinanceQueryBService {

    @Autowired
    ZsOrderWorkQueryBService zsOrderWorkQueryBService;
    @Autowired
    private OrderWorkListBService orderWorkListBService;

    @StaffOperateRecord(staffId = "{{#query.staffId}}",
            event = EventEnum.WORK_ORDER_QUERY,
            mapping = {
            @RecordField(field = RecordFieldEnum.STAFF_IP, value = "{{#query.ip}}"),
            @RecordField(field = RecordFieldEnum.PLAT, value = "{{#query.platWork}}")
    })
    @Override
    public List<ZsOrderWorkHqFinanceVO> listPageOrderWorkByQuery(OrderWorkQuery query) {
        return listPageOrderWorkByQueryNoRecord(query);
    }

    @Override
    public List<ZsOrderWorkHqFinanceVO> listPageOrderWorkByQueryNoRecord(OrderWorkQuery query) {

        zsOrderWorkQueryBService.processWorkQuery(query);

        // 查询
        List<EsOrderWork> esOrderWorks = orderWorkListBService.listPageOrderWorkByQuery(query);

        // 转 vo
        List<ZsOrderWorkHqFinanceVO> voList = Lists.newArrayList();
        esOrderWorks.forEach((esOrderWork -> {
            ZsOrderWorkHqFinanceVO vo = BeanMapper.map(esOrderWork, ZsOrderWorkHqFinanceVO.class);
            // 若：无需结算，则到账结算金额默认即：订单收入
            if (Objects.equals(vo.getChannelSettleStatus(), FicoConsts.SETTLE_STATUS_FAIL)) {
                vo.setSettleAmount(esOrderWork.getTotalAmount());
            }
            vo.setWorkId(esOrderWork.getId());
            voList.add(vo);
        }));
        return voList;
    }

    @Override
    public String exportListOrderWorkByQuery(OrderWorkQuery query) {
        return null;
    }
}
